<template>
  <!-- 薪酬清单--收入管理 -->
  <div class="compensation">
    <!-- <el-form :inline="true"
             :model="dataForm"
             @keyup.enter.native="getDataList()">
      <el-form-item prop="workerName">
        <el-input v-model="dataForm.workerName"
                  style="width: 330px"
                  placeholder="请输入姓名、手机号、身份证、所属单位"
                  clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   icon="el-icon-search"
                   @click="search()"></el-button>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="timeInterval"
                        type="daterange"
                        range-separator="-"
                        value-format="yyyy-MM-dd"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status"
                   clearable
                   placeholder="请选择确认状态">
          <el-option v-for="item in options"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.issuingState"
                   clearable
                   placeholder="请选择发放状态">
          <el-option v-for="item in options2"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="reset">重置</el-button>
        <el-button type="primary"
                   v-if="isAuth('sys:salary:save')"
                   @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <div class="file"
             v-if="isAuth('sys:salary:import')">
          <el-upload class="upload-demo"
                     :action="action"
                     :before-upload="beforeAvatarUpload"
                     :on-success="handleAvatarSuccess"
                     :headers='headers'
                     :show-file-list='false'
                     multiple
                     :limit="1"
                     :file-list="fileList">
            <el-button type="info"
                       class="btnColor">导入</el-button>
          </el-upload>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="info"
                   class="btnColor"
                   v-if="isAuth('sys:salary:export')"
                   :disabled="tableData.length <= 0"
                   @click="export2Excel()">导出</el-button>
      </el-form-item>
    </el-form> -->
    <!-- 表格开始 -->
    <el-table :data="tableData"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%">
      <!-- <el-table-column type="selection"
                       header-align="center"
                       align="center"
                       width="50">
      </el-table-column> -->
      <el-table-column prop="id"
                       fixed
                       header-align="center"
                       align="center"
                       width="72"
                       label="ID">
      </el-table-column>
      <el-table-column prop="taskName"
                       header-align="center"
                       align="center"
                       width="180"
                       label="任务名称">
      </el-table-column>
      <el-table-column prop="taskNo"
                       header-align="center"
                       align="center"
                       width="170"
                       label="任务编号">
      </el-table-column>
      <el-table-column prop="companyName"
                       header-align="center"
                       align="center"
                       width="225"
                       label="所属单位">
      </el-table-column>
      <el-table-column prop="workerName"
                       header-align="center"
                       align="center"
                       width="100"
                       label="姓名">
      </el-table-column>
      <el-table-column prop="mobile"
                       header-align="center"
                       align="center"
                       width="120"
                       label="手机号">
      </el-table-column>
      <el-table-column prop="cardCode"
                       header-align="center"
                       align="center"
                       width="170"
                       label="身份证号码">
      </el-table-column>
      <el-table-column prop="bankCartId"
                       header-align="center"
                       align="center"
                       width="180"
                       label="银行账户">
      </el-table-column>
      <el-table-column prop="bankName"
                       header-align="center"
                       align="center"
                       width="207"
                       label="开户银行">
      </el-table-column>
      <el-table-column prop="wage"
                       header-align="center"
                       width="139"
                       align="center"
                       label="税前收入">
      </el-table-column>
      <el-table-column prop="paymentTime"
                       header-align="center"
                       width="160"
                       align="center"
                       label="发放时间">
      </el-table-column>
      <el-table-column prop="paymentState"
                       header-align="center"
                       width="118"
                       align="center"
                       label="发放状态">
      </el-table-column>
      <el-table-column prop="taxManagerment"
                       header-align="center"
                       width="139"
                       align="center"
                       label="税收金额">
      </el-table-column>
      <!-- <el-table-column prop="taxManagerment"
                       header-align="center"
                       width="118"
                       align="center"
                       label="税收管理">
      </el-table-column> -->
      <el-table-column prop="taxRate"
                       header-align="center"
                       width="118"
                       align="center"
                       label="税收比例">
      </el-table-column>
      <el-table-column prop="taxCategories"
                       header-align="center"
                       width="118"
                       align="center"
                       label="税种">
      </el-table-column>
      <el-table-column prop="afterTax"
                       header-align="center"
                       width="139"
                       align="center"
                       label="税后金额">
      </el-table-column>
      <el-table-column prop="taxCategories"
                       header-align="center"
                       width="100"
                       fixed="right"
                       align="center"
                       label="确认状态">
        <template slot-scope="scope">
          <!-- 0-企业未确认， 1用户未确认，2用户确认 ,（收入5-企业未确认 , 6- 用户未确认,7-用户确认 ,  -->
          <span v-if="scope.row.status==0||scope.row.status==5">企业未确认</span>
          <span v-if="scope.row.status==1||scope.row.status==6">用户未确认</span>
          <span v-if="scope.row.status==2||scope.row.status==7">用户已确认</span>
        </template>
      </el-table-column>

    </el-table>

    <div class="pagination">
      <!-- 分页前说明 -->
      <div class="instructions">显示第{{tableData.length>0?((pageIndex-1)*pageSize+1):0}}到第{{(pageIndex-1)*pageSize+tableData.length}}条数据，总共{{totalPage}}条数据
        <!-- 条数据 -->
      </div>
      <!-- 分页 -->
      <el-pagination @size-change="sizeChangeHandle"
                     @current-change="currentChangeHandle"
                     :current-page="pageIndex"
                     :page-sizes="[10, 20, 50]"
                     :page-size="pageSize"
                     :total="totalPage"
                     layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 分页2 -->
    </div>

    <!-- 弹窗, 新增 / 修改 -->
    <addCompensation v-if="addOrUpdateVisible"
                     ref="addOrUpdate"
                     @refreshDataList="getDataList"></addCompensation>
  </div>
</template>

<script>
import addCompensation from '../modules/add/addCompensation'
export default {
  data () {
    return {
      dataForm: {
        workerName: '',
        status: '',
        issuingState: ''
      },
      headers: {
        token: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      // 时间区间
      timeInterval: '',
      startTime: '',
      endTime: '',
      // 假数据
      tableData: [],
      // 上传相关
      fileList: [],
      action: '',
      workerId: '',
      // 用户确认状态
      options: [
        {
          value: 0,
          label: '企业未确认'
        },
        {
          value: 1,
          label: '用户未确认'
        },
        {
          value: 2,
          label: '用户已确认'
        }
      ],
      options2: [
        {
          value: '已发放',
          label: '已发放'
        },
        {
          value: '未发放',
          label: '未发放'
        }
      ],
      taskNo: this.$route.query.taskNo
    }
  },
  components: {
    addCompensation
  },
  activated () {
    // 上传地址
    this.action = this.$baseUrl + '/sys/salary/import'
    this.headers.token = this.$cookie.get('token')
    this.getDataList()
  },
  watch: {
    $route: {
      handler () {
        this.taskNo = this.$route.query.taskNo
        this.getDataList()
      },
      deep: true
    },
    timeInterval () {
      try {
        this.startTime = this.timeInterval[0]
        this.endTime = this.timeInterval[1]
      } catch (error) {
        this.startTime = ''
        this.endTime = ''
      }
      this.getDataList()
    },
    // 监听用户确认状态变化
    'dataForm.status' () {
      this.pageIndex = 1
      this.getDataList()
    },
    'dataForm.issuingState' () {
      this.pageIndex = 1
      this.getDataList()
    }
  },
  methods: {
    // 获取数据列表
    async getDataList () {
      this.dataListLoading = true
      await this.$http({
        url: this.$http.adornUrl('/sys/worker/routeList'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'taskNo': this.taskNo
          // 'startTime': this.startTime,
          // 'endTime': this.endTime
        })
      }).then(({ data }) => {
        // console.log(data)
        this.dataListLoading = false
        this.tableData = data.page.list
        this.totalPage = data.page.totalCount
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增
    addOrUpdateHandle () {
      // 显示
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.mode = 'add'
        if (sessionStorage.getItem('userRoleId') === '2') {
          this.workerId = this.$store.state.user.id
        }
        this.$refs.addOrUpdate.dataForm = {
          bankCartId: '',
          bankName: '',
          cardCode: '',
          companyName: '',
          mobile: '',
          paymentState: '',
          paymentTime: '',
          taxCategories: '',
          taxManagerment: '',
          taxRate: '',
          // userId: 1,
          workerId: this.workerId,
          wage: '',
          workerName: '',
          taskName: '',
          status: '',
          isAuth: ''
        }
        this.$refs.addOrUpdate.addInit()
      })
    },
    // 编辑
    update (id) {
      // 显示
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.mode = 'edit'
        this.$refs.addOrUpdate.visible = true
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/salary/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.pageIndex = 1
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => { })
    },
    // 导入函数
    // 上传前
    beforeAvatarUpload (f) {
      if (!(f.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || f.type === 'application/vnd.ms-excel')) {
        this.$message.error('上传文件只能是 xls和xlsx 格式!')
        return false
      } else {
        return true
      }
    },
    // 导入成功处理函数
    handleAvatarSuccess (res, file) {
      if (res.code === 0) {
        this.$message.success(res.msg)
        this.pageIndex = 1
        this.getDataList()
      } else {
        this.$message.error(res.msg)
      }
      this.fileList = []
    },
    // 导出函数
    export2Excel () {
      this.$http({
        url: this.$http.adornUrl('/sys/salary/export'),
        method: 'post',
        responseType: 'blob',
        data: this.$http.adornParams({
          'startTime': this.startTime,
          'endTime': this.endTime,
          'workerName': this.dataForm.workerName
        })
      }).then((_res) => {
        let blob = new Blob([_res.data], {
          type: 'application/vnd.ms-excel'
        })
        let url = window.URL.createObjectURL(blob)
        let a = document.createElement('a')
        a.href = url
        a.download = '薪酬清单.xlsx'
        a.click()
        window.URL.revokeObjectURL(a.href)
      })
    },
    // 按钮搜索
    search () {
      this.pageIndex = 1
      this.getDataList()
    },
    // 重置
    reset () {
      this.pageIndex = 1
      this.dataForm.workerName = ''
      this.timeInterval = ''
      this.dataForm.status = ''
      this.dataForm.issuingState = ''
      this.getDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
.compensation {
  .pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .instructions {
      height: 19px;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 19px;
      color: #333333;
    }
  }
  // 导入
  button.el-button.btnColor.el-button--info.el-button--medium {
    position: relative;
    overflow: hidden;
  }
  .file {
    display: inline;
    position: relative;
    overflow: hidden;
    .fileBtn {
      opacity: 0;
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }
    .upload-demo {
      display: inherit;
    }
  }
}
</style>